<template>
  <div class="shouPiaoYuanPKCYXC">
    <basic-container>
      <avue-crud
        ref="crud"
        :option="option"
        :table-loading="listLoading"
        :data="list"
        :page.sync="page"
        @search-change="handleFilter"
        @selection-change="selectionChange"
        @refresh-change="getList"
        @on-load="refreshChange">
        <template slot="searchMenu" slot-scope="{row,size}">
          <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm)}" icon="el-icon-delete">清空</el-button>
        </template>
        <template slot-scope="scope" slot="lineCodeSearch">
          <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" clearable filterable>
            <el-option
              v-for="(item,index) in lineCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationCodeSearch">
          <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" clearable @change="changeStation" filterable>
            <el-option
              v-for="(item,index) in stationCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <!-- <template slot="search" slot-scope="{row,size}" >
          <el-form-item label="差异金额范围" labelWidth="150px" style="width:450px">
            <el-input-number v-model="searchForm.replyErrorReturnMoneyStart" :min="0" :max="99999998" label="差异金额范围" clearable></el-input-number>~
            <el-input-number v-model="searchForm.replyErrorReturnMoneyEnd" :min="1" :max="99999999" label="差异金额范围" clearable></el-input-number>
          </el-form-item>
        </template>-->
        <template slot-scope="scope" slot="equipmentNumberSearch">
          <el-select v-model="searchForm.equipmentNumber" placeholder="请选择" style="width: 150px">
            <el-option
              v-for="(item,index) in equipmentNumberList"
              :key="index"
              :label="item.operationCode"
              :value="item.operationCode">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="conductorPersonCodeSearch">
          <el-select v-model="searchForm.conductorPersonCode" placeholder="请选择" style="width: 150px" filterable>
            <el-option
              v-for="(item,index) in userList"
              :key="index"
              :label="item.nikename"
              :value="item.userCode">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="equipmentNumberSearch">
          <el-select v-model="searchForm.equipmentNumber" placeholder="请选择" style="width: 150px" filterable>
            <el-option
              v-for="(item,index) in equipmentNumberList"
              :key="index"
              :label="item.operationCode"
              :value="item.operationCode">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text" size="small" v-if="scope.row.jdReplySatus==1" plain @click.stop="revoke(scope.row)">撤回</el-button>
          <el-button type="text" size="small" v-if="scope.row.jdReplySatus!=1" plain @click.stop="previewEditor(scope.row,1)">协查回馈</el-button>
          <el-button type="text" size="small" plain @click.stop="previewEditor(scope.row,2)">预览</el-button>
        </template>
        <template slot="menuRight">
          <el-button type="primary"  size="mini" @click="submit">提交</el-button>
<!--          <el-button type="primary"  size="mini" @click="batchFeedback">批量回馈差异原因</el-button>-->
<!--          <el-button type="primary"  size="mini" @click="exportData">导出</el-button>-->
        </template>
      </avue-crud>
    </basic-container>
    <HuikuiInfo ref="HuikuiInfo" v-if="isShowDia"></HuikuiInfo>
    <HuikuiInfoBatchFeedback ref="HuikuiInfoBatchFeedback" v-if="isShowDiaBatchFeedback"></HuikuiInfoBatchFeedback>
  </div>

</template>

<script>
  import {getForm, postForm} from '@/api/gen/gen'
  import {mapGetters} from 'vuex';
  import HuikuiInfo from './HuikuiInfo';
  import HuikuiInfoBatchFeedback from './HuikuiInfoBatchFeedback';
  import {exportDataToExcel} from "../../../../../api/subwayRat/exportSelf";
  import {
    setSearchFiledsEmpty,
    getSwPmDeviceOperationCodeList,
    getSysUserToLS,
    getStationByCode,
    getLineAndStationByCode,
    getLineAllAsyn,
    getLineCodes
  } from "../../../../../api/subwayRat/commonCheck";
  import {tableOption,getSwAuConductorToBeCheckedList,updateSwAuConductorToBeCheckedToStatus,exportBOMXT,revokeBomPiaoKuan
  } from '../../../../../api/stationTiecketManager/shuJuXieChaGuanLi/BOM/XT/shouPiaoYuanPKCYXC';
  import { handleExcelResponse } from "@/util/util";
  export default {
    name: 'shouPiaoYuanPKCYXC',
    components:{
      HuikuiInfo,HuikuiInfoBatchFeedback
    },
    data() {
      return {
        isShowDia:false,
        isShowDiaBatchFeedback:false,
        option: tableOption,
        listLoading: true,
        selectKeys:[], //存放对像
        selectKeyVals:[], //存放值
        lineCodes:[],
        stationCodes:[],
        searchForm:{lineCode:'9',
          stationCode:'',
          conductorPersonCode:'',
          equipmentNumber:''},
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条,
          isAsc: false// 是否倒序
        },
        list : [],
        userList:[],
        showType : 0,
        equipmentNumberList : [],
        batchFeedbackList : []
      }
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    watch: {
    },
    mounted() {
     
      /*let p = {
        line_code : this.$store.getters.userInfo.lineCode,
        station_code : this.$store.getters.userInfo.stationCode
      };
      getSysUserToLS(p).then((response)=>{
        this.userList = response.data.data;
      });*/
      // getLineAllAsyn().then(res=>{ //获取线路
      //   this.lineCodes=res.data;
      // });
      getLineCodes().then(res=>{ //获取线路
        this.lineCodes=res;
      });
    },
    created() {
      this.changeLine("9")
      this.getSheBei();
    },
    methods: {
      setSearchFiledsEmpty,
      async submit(){
        if(this.selectKeys.length<=0){
          this.$message.warning("请选择要回馈的数据！");
          return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(1 == this.selectKeys[i].jdReplySatus){
            this.$message.warning("勾选数据中存在机电已回馈数据！");
            return
          }
        }
        let ids=[];
        for(let i=0;i<this.selectKeys.length;i++){
          ids.push(this.selectKeys[i].id);
        }
        let messageLSList = [];
        for(let i=0;i<this.selectKeys.length;i++){
          let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
          messageLSList.push({
            "content":"车站：" + data.data[0].name,
            "lineCode":this.selectKeys[i].lineCode,
            "stationCode":this.selectKeys[i].stationCode,
            "replyReason":this.selectKeys[i].jdReplyReason
          })
        }
        let p = {
          messageLSList:JSON.stringify(messageLSList),
          //title:this.$store.getters.tag.label,
	  title:'数据协查管理-售票员票款差异协查回馈',
          msgType:"提交",
          type:"shouPiaoYuanPKCYXC_001",
          ids:ids,
          typeTwo:"1",
          roleCode: 29
        };
        await updateSwAuConductorToBeCheckedToStatus(p).then((response)=>{
        });
        this.$message.success("操作成功");
        this.getList();
      },
      batchFeedback(){
        if(this.selectKeys.length<=0){
          this.$message.warning("请选择要回馈的数据！");
          return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(1 == this.selectKeys[i].jdReplySatus){
            this.$message.warning("勾选数据中存在机电已回馈数据！");
            return
          }
        }
        this.batchFeedbackList = this.selectKeys;
        this.isShowDiaBatchFeedback = true;
      },
      selectionChange(list){
        this.selectKeys=list;
        this.selectKeyVals=[];
        for(let i=0;i<list.length;i++){
          this.selectKeyVals.push(list[i].id);
        }
      },
      // async exportData(){ // 导出
      //   this.searchForm.pageNo=1;
      //   this.searchForm.pageSize=1000000000;
      //   if(this.selectKeys.length>0){
      //     exportDataToExcel(this.selectKeys,this.option.column,'BOM系统数据');
      //   }else {
      //     this.searchForm.jdReplyReason = "2";
      //     this.searchForm.jdReplyStatus = "1";
      //     this.searchForm.lineCode = this.$store.getters.userInfo.lineCode;
      //     this.searchForm.stationCode = this.$store.getters.userInfo.stationCode;
      //     let res=await getSwAuConductorToBeCheckedList(this.searchForm);
      //     let data = res.data.data;
      //     exportDataToExcel(data,this.option.column,'BOM系统数据');
      //   }
      // },
      async exportData(){ //导出
        let loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        })
        try {
          if(this.selectKeys.length>0){
            const res = await exportBOMXT({ids:this.selectKeyVals.join(",")});
            handleExcelResponse(res);
          }else{
            this.searchForm.assistState = "2,3";//导出协查中的和已协查的
            const res = await exportBOMXT(this.searchForm);
            handleExcelResponse(res);
          }
        } finally {
          loading.close();
        }
      },

      refreshChange() {
        this.getList()
      },

      previewEditor(row,type){
        this.showType = type;
        this.isShowDia=true;
        this.$nextTick(()=>{
          this.$refs.HuikuiInfo.recallShow(row);
        })
      },
      //撤回BOM票款
      revoke(row){
        let th=this;
        revokeBomPiaoKuan({ids:row.id}).then(res=>{
          if(res.data.code==0){
            th.$message.success("操作成功！");
            th.getList();
          }else{
            th.$message.error("操作失败！");
          }
        })
      },
      async getSheBei(){
        let res=[];
        await getSwPmDeviceOperationCodeList({lineCode:this.$store.getters.userInfo.lineCode,stationCode:this.$store.getters.userInfo.stationCode}).then(r=>{
          res=r.data.data;
        });
        this.equipmentNumberList=res;
        return res;
      },
      getList() {
        
        this.listLoading = true;
        Object.assign(this.searchForm,this.page);
        // this.searchForm.jdReplyReason = "2";
        // this.searchForm.jdReplyStatus = "1";
        // this.searchForm.lineCode = this.$store.getters.userInfo.lineCode;
        // this.searchForm.stationCode = this.$store.getters.userInfo.stationCode;
        //this.searchForm.assistStatess = "2,3,4,5,6,7,8,9";//机电协查状态
	this.searchForm.assistCategoryRole='jd';
        getSwAuConductorToBeCheckedList(this.searchForm).then((response)=>{
          this.list = response.data.data.list;
          this.listLoading = false;
          this.page.total=response.data.data.pageInfo.total;
        });
      },
      handleFilter(params, done) {
        Object.assign(this.searchForm,params);
        this.getList();
        done()
      },
      changeLine(v){ //改变线路时
        getStationByCode({line_code:v}).then(res=>{
          this.stationCodes=res.data;
          this.searchForm.stationCode='';
          this.searchForm.conductorPersonCode = '';
          this.searchForm.equipmentNumber = '';
        });
      },
      changeStation(v){
        getSysUserToLS({stationCode:v}).then((res)=>{
          this.userList=res.data.data;
          this.searchForm.conductorPersonCode = '';
        });
        getSwPmDeviceOperationCodeList({stationCode:v}).then((res)=>{
          this.equipmentNumberList=res.data.data;
        });
      },
    }
  }
</script>
